/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * codecUINew.java
 *
 * Created on 2011-5-27, 14:53:43
 */
package junchangmis.codecSetting;

import junchangmis.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.StringTokenizer;
import java.util.Vector;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.MouseListener;

/**
 *
 * @author developers
 */
public class codecUINew extends javax.swing.JFrame implements WindowListener {

    MainFrame parent;
    DefaultListModel codeListModel = new DefaultListModel();
    Vector<String> codes = new Vector<String>();
    Vector<String> meanings = new Vector<String>();

    /** Creates new form codecUINew */
    public codecUINew(MainFrame parent) {
        this.parent = parent;
        initComponents();
        this.jListCode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.jListCode.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (jListCode.getSelectedIndex() != -1) {
                    if (e.getClickCount() == 1) {
                        oneNewClick(jListCode.getSelectedValue());
                    }
                    if (e.getClickCount() == 2) {
                        twoNewClick(jListCode.getSelectedValue());
                    }
                }
            }
        });
        updateList();
        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        this.addWindowListener(this);
        this.setLocationRelativeTo(null);
        this.setTitle("学生编码管理");

    }

    public void windowActivated(WindowEvent arg0) { //设置为活动窗口时触发
        //System.out.println("windowActivated -->窗口被选中。");
    }

    public void windowClosed(WindowEvent arg0) { //窗口被关闭时触发
        //System.out.println("windowClosed -->窗口被关闭。");
        //parent.setVisible(true);
    }

    public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮
        System.out.println("windowClosing -->窗口关闭。");
        parent.setVisible(true);
    }

    public void windowDeactivated(WindowEvent arg0) { //设置为非活动窗口时触发
        //System.out.println("windowDeactivated -->取消窗口选中。");
    }

    public void windowDeiconified(WindowEvent arg0) { //窗口从最小化还原时触发
        //System.out.println("windowDeiconified -->窗口从最小化恢复。");
    }

    public void windowIconified(WindowEvent arg0) { //窗口最小化时触发
        //System.out.println("windowIconified -->窗口最小化。");
    }

    public void windowOpened(WindowEvent arg0) { //窗口最小化时触发
        //System.out.println("windowv -->窗口被打开。");
    }

    void oneNewClick(Object value) {
    }

    void twoNewClick(Object value) {
    }

    void updateList() {
        codecMethodNew.getAll(codes, meanings);
        this.codeListModel.removeAllElements();
        for (int i = 0; i < codes.size(); i++) {
            this.codeListModel.addElement(codes.get(i) + " " + meanings.get(i));
            this.jListCode.updateUI();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelInquire = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListCode = new javax.swing.JList();
        jButtonDelete = new javax.swing.JButton();
        jPanelAdd = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButtonAdd = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldClassCode = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldMeaning = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jTabbedPane1.setFont(new java.awt.Font("微软雅黑", 1, 24)); // NOI18N

        jListCode.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jListCode.setModel(codeListModel);
        jScrollPane1.setViewportView(jListCode);

        jButtonDelete.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jButtonDelete.setText("删除");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelInquireLayout = new javax.swing.GroupLayout(jPanelInquire);
        jPanelInquire.setLayout(jPanelInquireLayout);
        jPanelInquireLayout.setHorizontalGroup(
            jPanelInquireLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelInquireLayout.createSequentialGroup()
                .addContainerGap(277, Short.MAX_VALUE)
                .addComponent(jButtonDelete)
                .addGap(292, 292, 292))
            .addGroup(jPanelInquireLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 599, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanelInquireLayout.setVerticalGroup(
            jPanelInquireLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelInquireLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonDelete)
                .addContainerGap())
        );

        jTabbedPane1.addTab("查看", jPanelInquire);

        jPanelAdd.setLayout(new java.awt.BorderLayout());

        jButtonAdd.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jButtonAdd.setText("添加");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel1.setText("班级编码");

        jTextFieldClassCode.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N

        jLabel2.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jLabel2.setText("班级名称");

        jTextFieldMeaning.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(155, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldClassCode)
                    .addComponent(jTextFieldMeaning, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(112, 112, 112))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldClassCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldMeaning, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(jButtonAdd)
                .addContainerGap(88, Short.MAX_VALUE))
        );

        jPanelAdd.add(jPanel2, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("添加", jPanelAdd);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        // TODO add your handling code here:
        String classCode = this.jTextFieldClassCode.getText();
        String meaning = this.jTextFieldMeaning.getText();
        if (isBlank(classCode) == true || isBlank(meaning) == true) {
            JOptionPane.showConfirmDialog(null, "输入项目为空，添加失败！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
        } else if (codecMethodNew.hasCode(classCode) == true) {
            JOptionPane.showConfirmDialog(null, "项目重复，添加失败！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
        } else {
            codecMethodNew.addCode(classCode, meaning);
            updateList();
            JOptionPane.showConfirmDialog(null, "添加成功！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
            this.jTextFieldClassCode.setText(null);
            this.jTextFieldMeaning.setText(null);
        }
    }//GEN-LAST:event_jButtonAddActionPerformed

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        // TODO add your handling code here:
        if (this.jListCode.getSelectedIndex() != -1) {
            String content = this.jListCode.getSelectedValue().toString();
            StringTokenizer st = new StringTokenizer(content, " ");
            Vector<String> temp = new Vector<String>();
            while (st.hasMoreTokens()) {
                temp.add(st.nextToken());
            }
            String code = temp.get(0);
            codecMethodNew.deleteCode(code);
            updateList();
            JOptionPane.showConfirmDialog(null, "删除成功！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showConfirmDialog(null, "没有选中任何条目！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButtonDeleteActionPerformed
    boolean isBlank(String str) {
        if (str == null || str.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
        new codecUINew().setVisible(true);
        }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jListCode;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelAdd;
    private javax.swing.JPanel jPanelInquire;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextFieldClassCode;
    private javax.swing.JTextField jTextFieldMeaning;
    // End of variables declaration//GEN-END:variables
}
